<?php
 /*
 *	阿里巴巴管理模块
 */
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
use app\common\model\Alibaba as A;
use extend\Aliopen as AliOpen;
use app\common\model\ClothMain as CLOTH_M;


class Alibaba extends Base{
	protected $middleware = ['AdminCheck','AdminPermission'];
	
	public function initialize(){
		parent::initialize();
	}	
	
	// 更新阿里巴巴价格
	public function priceUpdate(){
		//return $this->getJson(6000);
		$res=A::ali_price_sub(78);
		return $this->getJson(['msg'=>'更新成功！','data'=>[]]);
		
		$data=[];
		$list=CLOTH_M::where('alibaba_id','<>','')->field('id,number')->select()->toArray();
		foreach($list as $T){
			$res=A::ali_price_sub($T['id']);
			if($res==true){
				$data[$T['number']]=true;
			}else{
				$data[$T['number']]=false;
			}
		}
		return $this->getJson(['msg'=>'更新成功！','data'=>$data]);
	}
	
	//更新产品关联ID
	public function relation(){
		//return $this->getJson(6000);
		
		$p=I('post.p',1);
		$data=A::relation_alibaba($p);
		return $this->getJson(['msg'=>'更新成功！','data'=>['page'=>$data]]);
	}
	
	// 拉取云端信息
	public function pullData(){
		//return $this->getJson(6000);
		
		$up=I('get.up',0);		//1为直接更新，0为返回数据不更新
		$option=I('get.option','');
		$id=I('get.id',0,'intval');	
		$ali_id=I('get.aid',0,'intval');
		$obj=new \Aliopen();
		
		if($ali_id==0 || $id==0) return $this->getJson(6001);
		$id=0;
		$res=$obj->get_product($ali_id);
		
		if(isset($res['error_message'])){
			return $this->getJson(['code'=>6000,'msg'=>$res['error_message']]);
		}else{
			$data=A::ali_cloth_core_pull($res);

			if($option!=''){
				$data=$data[$option];
			}	
			return $this->getJson(['code'=>0,'data'=>$data]);
		}
	}
	
	// 推送信息到云端
	public function pushData(){
		
	}
}